package cn.b.sky.admin.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptAlgorithm {

  private static String MD5 = "md5";

  private static String SHA = "sha";

  private static final int BUFFER_SIZE = 3;
  
  private static final int BUFFER_COMPARE = 4;
  
  private static final int BUFFER_FLOAT = 16;
  
  private static final String DECODE = "ISO-8859-1";
  
  private EncryptAlgorithm(){
    
  }
  /**
   * 
   * <strong>Title : hexMD5<br></strong>
   * <strong>Description : </strong>得到MD5加密后的str<br> 
   * <strong>Create on : 2016-4-14 下午2:27:08<br></strong>
   * <p>
   * @param str
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   * @throws NoSuchAlgorithmException 
   * @throws UnsupportedEncodingException 
   */
  public static String hexMD5(String str) throws UnsupportedEncodingException {
    return toHex(encryptMD5(str.getBytes(DECODE)));
  }

  /**
   * 
   * <strong>Title : hexSHA<br></strong>
   * <strong>Description : </strong>得到sha加密后的str<br> 
   * <strong>Create on : 2016-4-14 下午2:27:20<br></strong>
   * <p>
   * @param str
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   * @throws NoSuchAlgorithmException 
   * @throws UnsupportedEncodingException 
   */
  public static String hexSHA(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    return toHex(encryptSHA(str.getBytes(DECODE)));
  }


  /**
   * 
   * <strong>Title : encryptMD5<br></strong>
   * <strong>Description : </strong>MD5 加密<br> 
   * <strong>Create on : 2016-4-14 下午2:27:33<br></strong>
   * <p>
   * @param data
   * @return
   * @throws NoSuchAlgorithmException
   * byte[]
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private static byte[] encryptMD5(byte[] data) {
    MessageDigest md5;
    try {
      md5 = MessageDigest.getInstance(MD5);
    } catch (NoSuchAlgorithmException e) {
      throw new IllegalArgumentException("No such algorithm [" + MD5 + "]", e);
    }
    md5.update(data);
    return md5.digest();
  }

  /**
   * 
   * <strong>Title : encryptSHA<br></strong>
   * <strong>Description : </strong>SHA 加密<br> 
   * <strong>Create on : 2016-4-14 下午2:27:42<br></strong>
   * <p>
   * @param data
   * @return
   * byte[]
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   * @throws NoSuchAlgorithmException 
   */
  private static byte[] encryptSHA(byte[] data) throws NoSuchAlgorithmException {
    MessageDigest sha = MessageDigest.getInstance(SHA);
    sha.update(data);
    return sha.digest();
  }

  
  private static String toHex(byte[] buffer) {
    StringBuilder sb = new StringBuilder(buffer.length * BUFFER_SIZE);
    for (int i = 0; i < buffer.length; i++) {
      sb.append(Character.forDigit((buffer[i] & 0xf0) >> BUFFER_COMPARE, BUFFER_FLOAT));
      sb.append(Character.forDigit(buffer[i] & 0x0f, BUFFER_FLOAT));
    }
    return sb.toString();
  }

  /**
   * 
   * <strong>Title : getBASE64<br></strong>
   * <strong>Description : </strong>BASE64加密<br> 
   * <strong>Create on : 2015-8-6 下午4:27:22<br></strong>
   * <p>
   * @param str
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   * @throws UnsupportedEncodingException 
   */
  public static String getBASE64(String str) throws UnsupportedEncodingException {
    if (StringUtils.isBlank(str)){
      return "";
    }
    return Base64.encodeBase64String(str.getBytes(DECODE));
  }

  /**
   * 
   * <strong>Title : getFromBASE64<br></strong>
   * <strong>Description : </strong>BASE64解密<br> 
   * <strong>Create on : 2015-8-6 下午4:32:18<br></strong>
   * <p>
   * @param str
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  public static String getFromBASE64(String str) {
    if (StringUtils.isBlank(str)){
      return "";
    }
    return new String(Base64.decodeBase64(str));
  }

  public static void main(String[] args) throws Exception{
    System.out.println(hexMD5("Aa123456"));
  }
}